<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/12/30
 * Time: 15:32
 */
class apiorder extends MY_Controller
{
    function __construct()
    {
        parent::__construct();
        //加载维修订单模型
        $roleAry = array('lyb');
        if (!in_array($this->users['account'], $roleAry)) {
            js_goto('', 'You are not liyangbo！', '');
            return;
        }
        $this->load->model('api_model');
    }
    public function index(){
        $this->apilist();
    }

    /**
     * api申请列表
     * @param string $msg
     */
    public function apilist($msg=''){
        $data['apilist']=$this->api_model->apilist();
        $data['msg']=$msg;
        $this->load->view('defaults/apilist',$data);
    }

    /**
     * 授权app
     *
     */
    public function authorizeapp($type,$id){
        $data=array();
        $query=$this->api_model->apilist('','','','','','',$id);
        $status=$query[0]['status'];
        switch($type){
            case 'sq':
                if($status==2){
                    js_goto('orders/apiorder','请不要重复授权！');
                    break;
                }
                $str=$this->getRandomStr('s'.time(),7);
                $appid=$query[0]['apitype'].$str.$id;
                $appsecret=sha1($this->getRandomStr().time().$query[0]['url'].$id);
                $data=array(
                    'status'=>2,
                    'appid'=>$appid,
                    'appsecret'=>$appsecret,
                );
                $msg="授权成功！";
                break;
            case 'jf':
                if($status==3){
                    js_goto('orders/apiorder','请不要重复禁封！');
                    break;
                }
                $data=array(
                    'status'=>3
                );
                $msg="禁封成功！";
                break;
            case 'qy':
                if($status==2){
                    js_goto('orders/apiorder','请不要重复启用！');
                    break;
                }
                $data=array(
                    'status'=>2
                );
                $msg="启用成功！";
                break;
        }
        if($data) {
            $this->api_model->accreditapi($id, $data);
            $this->apilist($msg);
        }else{
            js_goto('','别瞎搞！','1');
        }
    }
    /**
     * 随机生成16位字符串
     * @return string 生成的字符串
     */
    function getRandomStr($str_pol='',$num=16)
    {
        $str = "";
        $max = strlen($str_pol) - 1;
        for ($i = 0; $i < $num; $i++) {
            $str .= $str_pol[mt_rand(0, $max)];
        }
        return $str;
    }

    /**
     * 测试用例
     */
    public function TestDemo(){
        $appId='DepositRecord14523319238';
        $secret='09a640196dfd72dfa33918944048e657aa9ab816';
        $getTokenResult = $this->TestGetToken($appId,$secret);
        //判断是否获取到token 如需要获取token超时时间 $getTokenResult['expires_in'] 默认7200秒
        if($getTokenResult['access_token']){
            $postDepositRecordResult=$this->TestPostDepositRecord($getTokenResult['access_token']);
            gsh_output($postDepositRecordResult);
        }else{
            gsh_output($getTokenResult);
        }
    }
    /**
     * 测试凭证获取
     * @param string $grant_type
     * @param string $appId
     * @param string $secret
     */
    public function TestGetToken($appId='',$secret='',$grant_type='client_credential'){
        $Url="http://oms.tomrepair.es/sc/maintainApi/Apiapplication/GetToken?grant_type=$grant_type&appId=$appId&secret=$secret";
        $getTokenResult=UrlGet($Url);
        return $getTokenResult;
    }

    /**
     * 测试提交付款记录
     * @param string $access_token
     */
    public function TestPostDepositRecord($access_token=''){
        $data=array(
            'data'=>array(
                'email'=>'775845301@qq.com',
                'money'=>'11.99',
                'time'=>1464240401,
                'name'=>'Edison',
                'currency'=>'USD',
                'orderId'=>'TEST100000010',
            ),
            'access_token'=>$access_token,
        );
        $Url="http://oms.tomrepair.es/sc/maintainApi/DepositApi/PostDepositRecord";
        $data=CurlPost($Url,$data);
        return $data;
    }
}